package com.fone.player.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.fone.player.R;
import com.fone.player.client.Reporter;
import com.fone.player.client.Reporter.EventId;
import com.fone.player.client.SpecllistRst.Col;
import com.fone.player.client.SpecllistRst.Lb;
import com.fone.player.client.SpecllistRst.Lbs;
import com.fone.player.util.L;
import com.fone.player.util.StringUtil;
import com.fone.player.view.FilmSingleSearch.OnTextClickListener;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class FilmSetSearch extends FrameLayout {

	/** 日志标签 */
	private static final String TAG = "FilmSetSearch";
	
	private View mView;

	/** 类型 */
	private FilmSingleSearch categoryStrip;
	/** 国家 */
	private FilmSingleSearch countryStrip;
	/** 年份 */
	private FilmSingleSearch yearStrip;
	/** 栏目 */
	private FilmSingleSearch attentionStrip;

	private ImageView categoryLine;
	private ImageView countryLine;
	private ImageView yearLine;
	private ImageView titleLine;

	private CheckBox hideType;

	/** 类别 */
	private String category = "全部";
	/** 国家 */
	private String country = "全部";
	/** 年份 */
	private String year = "全部";
	/** 栏目 */
	private String attention;
	
	private static final int CATEGORYNUM = 0;
	private static final int COUNTRYNUM = 1;
	private static final int YEARNUM = 2;

	private List<String> arrayCategory = new ArrayList<String>();
	private List<String> arrayCountry = new ArrayList<String>();
	private List<String> arrayYear = new ArrayList<String>();
	private List<String> arrayAttention = new ArrayList<String>();
	
	private boolean showCategory = false;
	private boolean showCountry  = false;
	private boolean showYear  = false ;
	private boolean showAttention  = false;
	
	private HashMap<String, String> mapColumn = new HashMap<String,String>();
	

	private OnSearcherListener onSearcherListener;

	public FilmSetSearch(Context context) {
		this(context, null);
	}

	public FilmSetSearch(Context context, AttributeSet attrs) {
		super(context, attrs);
		mView = LayoutInflater.from(context).inflate(R.layout.view_film_search, this, true);
//		initView();
		initView();
	}
	
	/** 初始化"分类检索"中 搜索 */
	public void initSearch(List<Lbs> lbsList){
		if(lbsList.size() ==1){
			arrayCategory.clear();
			for (Lb lb : lbsList.get(CATEGORYNUM).lb) {
				arrayCategory.add(lb.name);
			}
			if (lbsList.get(CATEGORYNUM).lb.size() >1) {
				showCategory = true;
			} else {
				showCategory = false;
			}
			showCountry  = false;
			showYear     = false;
		}else if(lbsList.size() ==2){
			
			arrayCategory.clear();
			for (Lb lb : lbsList.get(CATEGORYNUM).lb) {
				arrayCategory.add(lb.name);
			}
			if (lbsList.get(CATEGORYNUM).lb.size() >1) {
				showCategory = true;
			} else {
				showCategory = false;
			}
			
			arrayCountry.clear();
			for (Lb lb : lbsList.get(COUNTRYNUM).lb) {
				arrayCountry.add(lb.name);
			}
			
			if (lbsList.get(COUNTRYNUM).lb.size() >1) {
				showCountry = true;
			} else {
				showCountry = false;
			}
			
	
			showYear     = false;
			
		}else{
			int cont = 0;
			arrayCategory.clear();
			for (Lb lb : lbsList.get(CATEGORYNUM).lb) {
				if (!StringUtil.isEmpty(lb.name)) {
					arrayCategory.add(lb.name);
					if (!lb.name.equals("全部")){
						cont++;
					}
				}
				
			}
			
			if (cont >0) {
				showCategory = true;
			} else {
				showCategory = false;
			}
			
			cont = 0;
			arrayCountry.clear();
			for (Lb lb : lbsList.get(COUNTRYNUM).lb) {
				
				if (!StringUtil.isEmpty(lb.name)) {
					arrayCountry.add(lb.name);
					if (!lb.name.equals("全部")){
						cont++;
					}
				}
			}
			
			if (cont >0) {
				showCountry = true;
			} else {
				showCountry = false;
			}
			
			cont = 0;
			arrayYear.clear();
			for (Lb lb : lbsList.get(YEARNUM).lb) {
				
				if (!StringUtil.isEmpty(lb.name)) {
					arrayYear.add(lb.name);
					if (!lb.name.equals("全部")){
						cont++;
					}
				}
			}
			
			if (cont >0) {
				showYear = true;
			} else {
				showYear = false;
			}
		}
		
		frashSearch();
	}
	
	/** 初始化"分类检索"中 栏目 */
	public void initColumn(List<Col> attentionList){
		mapColumn.clear();
		arrayAttention.clear();
		for (Col col : attentionList) {
			if(col.type == 1){
				arrayAttention.add(col.name);
				mapColumn.put(col.name, col.url);
			}
		}
		
		if (attentionList.size() > 1) {
			showAttention = true;
		} else {
			showAttention = false;
		}
		
		frashColumn();
	}
	
//	public void initData(List<Lb> categoryList, List<Lb> countryList, List<Lb> yearList, List<Col> attentionList) {
//		arrayCategory.clear();
//		for (Lb lb : categoryList) {
//			arrayCategory.add(lb.name);
//		}
//
//		arrayCountry.clear();
//		for (Lb lb : countryList) {
//			arrayCountry.add(lb.name);
//		}
//
//		arrayYear.clear();
//		for (Lb lb : yearList) {
//			arrayYear.add(lb.name);
//		}
//		
//		if(attentionList != null && attentionList.size() >0){
//			for (Col col : attentionList) {
//				arrayAttention.add(col.name);
//				mapColumn.put(col.name, col.url);
//			}
//		}
//
//		frashView();
//	}
	
	/** 刷新"分类检索"中 搜索 */
	private void frashSearch() {
		
		categoryStrip.setViewPager(arrayCategory);
		countryStrip.setViewPager(arrayCountry);
		yearStrip.setViewPager(arrayYear);
		
	}
	
	/** 刷新"分类检索"中 栏目 */
	private void frashColumn() {
		attentionStrip.setViewPager(arrayAttention);
	}
	
	private void initView() {
		
	    TextView tv = (TextView) mView.findViewById(R.film.title);
	    
	    tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
					if (hideType.isChecked()) {
						hideType.setChecked(false);
						hideView();
					} else {
						hideType.setChecked(true);
						showView();
					}
				
				
			}});
	    
		hideType = (CheckBox) mView.findViewById(R.film.hideType);

		categoryLine = (ImageView) mView.findViewById(R.film.line_category);
		countryLine = (ImageView) mView.findViewById(R.film.line_country);
		yearLine = (ImageView) mView.findViewById(R.film.line_year);
		titleLine = (ImageView) mView.findViewById(R.film.line_title);

		hideType.setOnCheckedChangeListener(new ShowListener());

		// 类别
		categoryStrip = (FilmSingleSearch) mView.findViewById(R.film.category);
		categoryStrip.setViewPager(arrayCategory);
		categoryStrip.OnTextClickListener(new OnTextClickListener() {
			@Override
			public void onClick(String str) {
				category = str;
				L.v(TAG, "frashView()", str);
				onSearcherListener.onClick(category, country, year, "");
			}
		});
		
		// 国家
		countryStrip = (FilmSingleSearch) findViewById(R.film.country);
		countryStrip.setViewPager(arrayCountry);
		countryStrip.OnTextClickListener(new OnTextClickListener() {
			@Override
			public void onClick(String str) {
				country = str;
				L.v(TAG, "frashView()", str);
				onSearcherListener.onClick(category, country, year, "");
			}
		});

		// 年代
		yearStrip = (FilmSingleSearch) findViewById(R.film.year);
		yearStrip.setViewPager(arrayYear);
		yearStrip.OnTextClickListener(new OnTextClickListener() {
			@Override
			public void onClick(String str) {
				year = str;
				L.v(TAG, "frashView()", str);
				onSearcherListener.onClick(category, country, year, "");
			}
		});

		// 关注度
		attentionStrip = (FilmSingleSearch) findViewById(R.film.attention);
		attentionStrip.setViewPager(arrayAttention);

		attentionStrip.OnTextClickListener(new OnTextClickListener() {
			@Override
			public void onClick(String str) {
				attention = str;
				L.v(TAG, "frashView()", str);
				onSearcherListener.onClick("", "", "", mapColumn.get(attention));
				hideType.setChecked(false);
			}
		});
	}

	private void showView() {
		
		if (showCategory) {
			categoryStrip.setVisibility(View.VISIBLE);
			categoryLine.setVisibility(View.VISIBLE);
		}
		if (showCountry) {
			countryStrip.setVisibility(View.VISIBLE);
			countryLine.setVisibility(View.VISIBLE);
		}
		if (showYear) {
			yearStrip.setVisibility(View.VISIBLE);
			yearLine.setVisibility(View.VISIBLE);
			
		}
		if (showAttention) {
			attentionStrip.setVisibility(View.VISIBLE);
		}
		
		titleLine.setVisibility(View.GONE);
		
		
		
	}

	private void hideView() {
		categoryStrip.setVisibility(View.GONE);
		countryStrip.setVisibility(View.GONE);
		yearStrip.setVisibility(View.GONE);
		attentionStrip.setVisibility(View.GONE);

		categoryLine.setVisibility(View.GONE);
		countryLine.setVisibility(View.GONE);
		yearLine.setVisibility(View.GONE);
		titleLine.setVisibility(View.VISIBLE);
	}
	
	
	private class ShowListener implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			/* 搜索入口 */
			Reporter.logEvent(EventId.CLICK_CLASS_BTN);

			if (isChecked) {
				showView();
			} else {
				hideView();
			}
		}
		
	}
	

	// 检索接口
	public interface OnSearcherListener {
		void onClick(String category, String country, String year,
				String attention);
	}

	// 给分类检索监听
	public void setOnSearcherListener(OnSearcherListener onSearcherListener) {
		this.onSearcherListener = onSearcherListener;
	}

}
